package com.netease.nim.uikit.business.session.emoji;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.business.session.emoji.sentence.Book;
import com.netease.nim.uikit.business.session.emoji.sentence.SentenceAdapter;
import com.netease.nim.uikit.business.session.emoji.sentence.SimplePaddingDecoration;
import com.netease.nim.uikit.business.session.emoji.sentence.expandablerecyclerview.bean.RecyclerViewData;
import com.netease.nim.uikit.business.session.emoji.sentence.expandablerecyclerview.listener.OnRecyclerViewListener;
import com.netease.nim.uikit.common.leadbean.SentenceBean;
import com.netease.nim.uikit.common.leadbean.SentenceItemBean;
import com.netease.nim.uikit.common.util.log.LogUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 話術选择控件
 */
public class SentencePickerView extends LinearLayout implements ITextItemSelectedListener, OnRecyclerViewListener.OnItemClickListener {

    private Context context;

    private ITextItemSelectedListener listener;

    private boolean loaded = false;


    private Handler uiHandler;
    private RecyclerView rv_container;
    private ProgressBar propressBar;
    private List<RecyclerViewData> mDatas;

    public SentencePickerView(Context context) {
        super(context);

        init(context);
    }

    public SentencePickerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SentencePickerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    SentenceAdapter sentenceAdapter;

    private void init(Context context) {
        this.context = context;
        mDatas = new ArrayList<>();
        this.uiHandler = new Handler(context.getMainLooper());
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.translate_layout, this);
        //initBooks();
        sentenceAdapter = new SentenceAdapter(context, mDatas);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setupView();
        getDatas();
    }

    private void getDatas() {
        propressBar.setVisibility(View.VISIBLE);
        String sentence = getString("sentence");
        if (sentence != null) {
            List<SentenceBean> SentenceBeanlist = JSON.parseArray(sentence, SentenceBean.class);
            for (SentenceBean titleBean : SentenceBeanlist) {
                List<Book> tempItemData = new ArrayList<>();
                for (SentenceItemBean item : titleBean.getReplyList()) {
                    tempItemData.add(new Book(item.getName(),item.getAbbr()));
                }
                mDatas.add(new RecyclerViewData(titleBean.getName(), tempItemData, false));
            }
        }
        sentenceAdapter.setAllDatas(mDatas);
        propressBar.setVisibility(View.GONE);
    }

    private String getString(String key) {
        return getSharedPreferences().getString(key, null);
    }


    private SharedPreferences getSharedPreferences() {
        return context.getSharedPreferences("Demo", Context.MODE_PRIVATE);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }


    public void setListener(ITextItemSelectedListener listener) {
        if (listener != null) {
            this.listener = listener;
        } else {
            LogUtil.i("sticker", "listener is null");
        }
    }
    LinearLayoutManager linearLayoutManager;
    protected void setupView() {
        rv_container = findViewById(R.id.rv_container);
        linearLayoutManager = new LinearLayoutManager(context);
        rv_container.setLayoutManager(linearLayoutManager);
        //rv_container.addItemDecoration(new SimplePaddingDecoration(context));
        propressBar = findViewById(R.id.pb_progress);
        /*sentenceAdapter.setOnItemClickLitsener(new SentenceAdapter.onItemClickListener() {
            @Override
            public void onItemClick(String content, int position) {
                listener.onContentSelected(content,content);
            }
        });*/
        sentenceAdapter.setOnItemClickListener(this);
        rv_container.setAdapter(sentenceAdapter);
    }


    @Override
    public void onContentSelected(String key, String content) {
        listener.onContentSelected(key, content);
    }

    //List<View> expandLayouts = new ArrayList<>();
    @Override
    public void onGroupItemClick(int position, int groupPosition, View view) {
        //onGroupExpand
        /*if(expandLayouts.contains(view)){
            expandLayouts.remove(view);
            ImageView target = (ImageView) ((ViewGroup) view).getChildAt(1);
            if(target!=null)
                target.setEnabled(true);
        }else{
            expandLayouts.add(view);
            ImageView target = (ImageView) ((ViewGroup) view).getChildAt(1);
            if(target!=null)
                target.setEnabled(false);
        }*/
    }

    @Override
    public void onChildItemClick(int position, int groupPosition, int childPosition, View view) {
        TextView target = (TextView) ((ViewGroup)view).getChildAt(0);
        String content = (String) target.getTag();
        listener.onContentSelected(content,content);
    }

    @Override
    public void onGroupCollapse(int position) {
        LogUtil.i("wjk","onGroupCollapse"+position);
        View childAt = linearLayoutManager.findViewByPosition(position);
        if(childAt!=null) {
            View target = ((ViewGroup) childAt).getChildAt(1);
            if (target != null) {
                //target.setEnabled(true);
            }
        }
    }

    @Override
    public void onGroupExpand(int position) {
        LogUtil.i("wjk","onGroupExpand"+position);
        View childAt = rv_container.getChildAt(position);
        if(childAt!=null){
            View target = ((ViewGroup) childAt).getChildAt(1);
            if(target!=null) {
                //target.setEnabled(false);
            }
        }
    }
}
